﻿$(function () {
    show();
})
function show() {
    layui.use(function () {
        var table = layui.table;
        var requisitionNumber = $("#requisitionNumber").val();
        var requisitionDate = $("#requisitionDate").val();
        var state = $("#state").val();
        // 先进行表格的初始渲染
        table.render({
            elem: '#productionPickingtable',  // 表格元素的选择器
            page: {
                limit: 5,
                limits: [5, 8, 10]
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['exports'],
            cols: [[
                { type: 'checkbox', fixed: 'left', title: '复选框' },
                { field: 'requisitionNumber', title: '领料编号' },
                { field: 'requisitionName', title: '领料名称' },
                {
                    field: 'requisitionDate', title: '领料日期', templet: function (d) {
                        return PriorityState(d.state); // 调用自定义的时间格式转换函数
                    }
                },
                { field: 'requisitionPerson', title: '领料人' },
                { field: 'workOrderOrder', title: '工单编号' },
                { field: 'taskOrder', title: '任务编号' },
                { field: 'processRoute', title: '工艺路线' },
                { field: 'processName', title: '工序名称' },
                { field: 'quantityReceived', title: '领料总数' },
                {
                    field: 'state', title: '状态',templet: function (d) {
                        return PriorityState(d.state); // 调用自定义的时间格式转换函数
                  } },
                {
                    field: '', title: '操作', templet: function (d) {
                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.productionRequisitionId + ')"><i class="layui-icon layui-icon-edit"></i>查看</a> ' +
                            '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.productionRequisitionId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                    }

                }
            ]],
            // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
            data: []
        });
        $.ajax({
            url: "/productionPicking/ProductionOrdersShow",
            type: "get",
            data: {
                requisitionNumber: requisitionNumber,
                requisitionDate: requisitionDate,
                state: state,
                token: localStorage.getItem("token")
            },
            success: function (data) {

                data = JSON.parse(data);
                console.log(data.data)
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                table.reload('productionPickingtable', {
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    data: data.data
                });
            },
            error: function () {
                console.log("请求失败");
            }
        });
    });
}
window.editData = function (id) {
    localStorage.setItem("productionRequisitionId", id)
    console.log(id)
    window.location.href = "/productionPicking/productionPickingModelView";
}
window.UpdateData = function (id) {
    localStorage.setItem("productionRequisitionId", id)
    console.log(id)
    window.location.href = "/Delivery/DeliveryUpdateView";
}
window.deleteData = function (id) {
    console.log(id)
    $.ajax({
        url: '/productionPicking/productionPickingDelete',
        type: 'post',
        data: { data: JSON.stringify({ productionRequisitionId: id }), token: localStorage.getItem("token") },
        success: function (res) {
            var s = JSON.parse(res)
            console.log(res)
            if (s.data >= 1) {
                layer.alert("删除成功");
                setTimeout(function () {
                    location.reload();
                }, 2000);

            }
            else {
                layer.alert("删除失败");
            }
        }
    })
}
window.chuku = function (id) {
    console.log(id)
    $.ajax({
        url: '/Delivery/DeliveryUpdateState',
        type: 'post',
        data: { data: JSON.stringify({ deliveryId: id }), token: localStorage.getItem("token") },
        success: function (res) {
            var s = JSON.parse(res)
            console.log(res)
            if (s.data >= 1) {
                layer.alert("出库成功");
                setTimeout(function () {
                    location.reload();
                }, 2000);

            }
            else {
                layer.alert("出库失败");
            }
        }
    })
}

function formatTime(timeStamp) {
    if (timeStamp) {
        var date = new Date(timeStamp);
        var year = date.getFullYear();
        var month = ('0' + (date.getMonth() + 1)).slice(-2);
        var day = ('0' + date.getDate()).slice(-2);
        var hour = ('0' + date.getHours()).slice(-2);
        var minute = ('0' + date.getMinutes()).slice(-2);
        var second = ('0' + date.getSeconds()).slice(-2);
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    }
    return "";
}
function PriorityState(state) {
    debugger
    if (state == 0) {
        return "待审核"
    }
    else {
        return "已审核"
    }
}
function Add() {
    debugger;
    window.location.href = "/productionPicking/productionPickingAdd";
};